package com.example.happ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.happ.mapper.MerchantInfoMapper;
import com.example.happ.mapper.ReviewInfoMapper;
import com.example.happ.mapper.StoreInfoMapper;
import com.example.happ.pojo.Merchant;
import com.example.happ.pojo.Store;
import com.example.happ.pojo.StoreMerchant;
import com.example.happ.service.MerchantInfoService;
import com.example.happ.service.StoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MerchantInfoServiceImpl implements MerchantInfoService {
    @Resource
    private ReviewInfoMapper reviewInfoMapper;
    @Resource
    private MerchantInfoMapper merchantInfoMapper;
    @Resource
    private StoreInfoMapper storeInfoMapper;
    @Override
    public int  findMerchantInformationByOpenid(String openId) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("wechat_id",openId);
        Merchant merchant=merchantInfoMapper.selectOne(queryWrapper);
        return merchant==null?0:merchant.getId();
    }
    @Override
    public StoreMerchant insertMerchantInformationByOpenid(String openId, String avatarUrl, String nickName) {
        Merchant merchant = new Merchant();
        merchant.setWechatId(openId);
        merchant.setAvatar(avatarUrl);
        merchant.setNickname(nickName);
        merchant.setCreatedAt(new Date());
        merchant.setSystemDaysLeft(30);
        merchant.setAmount(0.00);
        merchantInfoMapper.insert(merchant);
        Store store = new Store();
        int id = findMerchantInformationByOpenid(openId);
        store.setMerchantId(id);
        store.setCreatedAt(new Date());
        storeInfoMapper.insert(store);
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("merchant_id",id);
        Store store1 = storeInfoMapper.selectOne(queryWrapper);
        return new StoreMerchant(id,store1.getId());
    }

    @Override
    public Merchant findAll(int id) {
        QueryWrapper queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return merchantInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer countReviewById(int id) {
        int count=reviewInfoMapper.selectReviewCount(id);
        return count;
    }

    @Override
    public int updateMerchantInfo(Merchant merchant, Map<String, String> requestBody) {
        merchant.setUpdatedAt(new Date());
        merchant.setAddress(requestBody.get("address"));
        merchant.setAgreement(Integer.valueOf(requestBody.get("agreement")));
        merchant.setAvatar(requestBody.get("avatar"));
        merchant.setNickname(requestBody.get("nickname"));
        merchant.setName(requestBody.get("name"));
        merchant.setPhone(requestBody.get("phone"));
        UpdateWrapper<Merchant> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",requestBody.get("id"));
        return merchantInfoMapper.update(merchant,updateWrapper);
    }

    @Override
    public Merchant selectAmountById(int id) {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return merchantInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean updateMerchantAmountInfo(Double amount, int merchantId) {
        return merchantInfoMapper.updateAmount(amount,merchantId)>0?true:false;
    }

    @Override
    public boolean updateMerchantAmountInfo1(Integer merchantId, Double amount) {
        return merchantInfoMapper.updateAmount1(amount,merchantId)>0?true:false;
    }

}
